/**
 * 系统消息
 */
import state from '../state/index.js';
const db = uniCloud.database();
export default {
	get: ({
		type,
		excludeType
	} = {}) => {
		const notificationDatas = state.platNotification.dataList
		if (notificationDatas) {
			return notificationDatas.reduce((sum, item) => {
				// 指定需要的类型
				if (type) {
					//兼容字符串和数组
					typeof type == 'string' ? type = [type] : ''
					if (type.includes(item.payload.subType)) {
						sum.push(item)
					}
					// 排查指定的类型
				} else if (excludeType) {
					//兼容字符串和数组
					typeof excludeType == 'string' ? excludeType = [excludeType] : ''
					if (!excludeType.includes(item.payload.subType)) {
						sum.push(item)
					}
				} else {
					sum.push(item)
				}
				return sum
			}, [])
		} else {
			return false
		}
	},
	async loadMore() {
		let res = await db.collection('wjf-platform-notification')
			.aggregate()
			.match('"payload.type" == "wjf-platform-notification" && "user_id" == $cloudEnv_uid')
			.sort({
				create_time: -1
			})
			.limit(1000)
			.end()
		console.log('读取平台通知库', res);
		this.add(res.result.data)
		this.hasMore == (res.result.data.length != 0)
	},
	add(datas) {
		if (!Array.isArray(datas)) {
			datas = [datas]
		}

		let notificationDatas = datas.concat(state.platNotification.dataList)
		// 正序，实现时间大的覆盖时间小的
		notificationDatas.sort((a, b) => a.create_time - b.create_time)
		// console.log('notificationDatas',notificationDatas);
		let obj = {}
		for (var i = 0; i < notificationDatas.length; i++) {
			let item = notificationDatas[i]
			// 去重操作
			let {
				subType,
				unique
			} = item.payload
			obj[unique ? (subType + "_" + unique) : (Date.now() + "_" + i)] = item
		}
		let dataList = []
		for (let key in obj) {
			let item = obj[key]
			dataList.push(item)
		}
		// 倒序 实现，最新的消息在最上面
		dataList.sort((a, b) => b.create_time - a.create_time)
		// console.log('dataList',dataList)
		state.platNotification.dataList = dataList
		this.unreadCount()
	},
	unreadCount(param = {}) {
		// console.log('读取未读通知数');
		let notificationDatas = this.get(param)
		let unreadCount = notificationDatas.reduce((sum, item, index, array) => {
			if (!item.is_read) {
				sum++
			}
			return sum
		}, 0)
		state.platNotification.unreadNum=unreadCount
		console.log('读取未读通知数',unreadCount);
		// console.log('最新的未读数:', unreadCount, data);
		try {
			// 注意：在非tabbar页面无法设置 badge
			if (unreadCount === 0) {
				uni.removeTabBarBadge({
					index: 2,
					complete: (e) => {
						// console.log(e)
					}
				})
			} else {
				console.log('设置tabbar-2');
				uni.setTabBarBadge({
					index: 2,
					text: unreadCount + '',
					complete: (e) => {
						// console.log(e)
					}
				})
			}
		} catch (e) {
			// console.log('set tabBarBadge',e);
		}

		if (unreadCount) {
			return unreadCount + ''
		} else {
			return ''
		}
	}
}